#pragma once
#include "nodes\DynamicSpriteNode.h"
#include <vector>
#include "utils\mtrand.h"
#include "cvar\CVar.h"
#include "game\World.h"
#include "collision\CollisionDetector.h"

class Meteoroids {

struct Meteoroid {
	ds::Sprite* sprite;
	ds::CDHandle handle;
	ds::Vec2 startPos;
	ds::Vec2 endPos;
	ds::Vec2 midPos;
	float timer;
	int side;
	ds::Vec2 lastSpawn;
	int energy;
};

struct TrailPart {
	ds::Sprite* sprite;
	float timer;
	ds::CDHandle handle;
};

typedef std::vector<Meteoroid> MeteoroidList;
typedef std::vector<Meteoroid>::iterator MeteoroidIterator;
typedef std::vector<TrailPart> TrailParts;

public:
	Meteoroids(ds::DynamicSpriteNode* spriteNode) : m_Sprites(spriteNode) , m_SpawnCount(5), m_SpawnTimer(0.0f) , m_CD(gEngine->getCollisionDetector()) {
		rand.seed(GetTickCount());
	}
	~Meteoroids(void);
	void init();
	void update(float elapsed);
	void createBouncing();
	void setSpawnCount(int sc) {
		m_SpawnCount = sc;
	}
	void start(int spawnCount,float spawnDelay) {
		m_SpawnCount = spawnCount;
		m_SpawnDelay = spawnDelay;
		m_SpawnTimer = 0.0f;
		clear();
	}
	void clear();
	void hit(ds::CDHandle handle);
private:
	ds::Vec2 spline(Meteoroid& meteoroid);
	ds::Vec2 createPosition(int side,float min,float max);
	//
	MeteoroidList m_Meteoroids;
	MTRand_open rand;
	ds::CollisionDetector& m_CD;
	ds::DynamicSpriteNode* m_Sprites;
	float m_SpawnTimer;
	int m_SpawnCount;
	TrailParts m_TrailParts;
	float m_SpawnDelay;
};

